package com.ghostflying.groundstation;

import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class Maps_implements implements OnMapLongClickListener,GooglePlayServicesClient.ConnectionCallbacks,GooglePlayServicesClient.OnConnectionFailedListener, OnInfoWindowClickListener, OnMarkerDragListener {
	
	private GoogleMap mmap = null;
	private static Marker long_press_point = null;
	private Handler mHandler = null;
	public Maps_implements(GoogleMap mMap,Handler handler) {
		// TODO Auto-generated constructor stub
		mmap = mMap;
		mHandler = handler;
	}
	
	public Maps_implements(GoogleMap mMap) {
		// TODO Auto-generated constructor stub
		mmap = mMap;
		//mHandler = handler;
	}
	
	public void setup_handler(Handler handler){
		mHandler = handler;
	}

	@Override
	public void onMapLongClick(LatLng point) {
		// TODO Auto-generated method stub
		
		if (long_press_point == null){
			long_press_point = mmap.addMarker(new MarkerOptions().position(point));
		}
		else {
			long_press_point.setPosition(point);
		}
		long_press_point.setTitle("WayPoint");
		long_press_point.setSnippet("Latitude:"+Double.toString(point.latitude)+"  Longitude:"+Double.toString(point.longitude));
		long_press_point.showInfoWindow();
		
		
	}

	@Override
	public void onConnectionFailed(ConnectionResult arg0) {
		// TODO Auto-generated method stub
		Log.d("Play Service", "Play Service Connection Failed.");
	}

	@Override
	public void onConnected(Bundle arg0) {
		// TODO Auto-generated method stub
		Log.d("Play Service", "Play Service is  Connected.");
	}

	@Override
	public void onDisconnected() {
		// TODO Auto-generated method stub
		Log.d("Play Service", "Play Service is Disconnected.");
	}


	@Override
	public void onInfoWindowClick(Marker marker) {
		// TODO Auto-generated method stub
		if (mHandler!=null){
			mHandler.obtainMessage(MainActivity.MapsControlFragment.MAP_MESSAGE_INFOWINDOW_CLICK,marker).sendToTarget();
		}
		
		Log.d("Maps", "The InfoWindowClick Click.");
	}

	@Override
	public void onMarkerDrag(Marker arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMarkerDragEnd(Marker marker) {
		// TODO Auto-generated method stub
		String title = marker.getTitle();
		int position = Integer.parseInt(title.substring(3));
		if (mHandler!=null){
			mHandler.obtainMessage(MainActivity.MapsControlFragment.MAP_MESSAGE_MARKER_DRAG,position).sendToTarget();
		}
		
		
		
	}

	@Override
	public void onMarkerDragStart(Marker arg0) {
		// TODO Auto-generated method stub
		
	}
	

}
